//是否在选课阶段
var isElective = undefined;

//记录当前选择的“课程周期”
var electiveWeek = 0;

//记录当前选择的“课程几次”
var electiveTime = 0;

var courseName = undefined;

var weekTitle = undefined;

var timeTitle = undefined;

var scheduleId = 0;

var adminClaszs;
var studyClaszs;

var log_type = 0;

function loadAdminStudyClasz() {
    $.getJSON('clasz/all-admin-claszs', function (data) {
        adminClaszs = data;
    });
    $.getJSON('clasz/all-study-claszs', function (data) {
        studyClaszs = data;
    });
}

$(function () {
    loadAdminStudyClasz();

    isElective = $("#is_elective").val();

    //初始化教师课表
    initialCourseTable();

    $('#course_study_layout').layout('setRegionToolVisibleState',{region:'south',visible:false});

    //设置“南布局”的标题
    if (isElective == 'true') {
        $("#course_study_layout").layout("panel", "south")
            .panel("setTitle", "课程学生列表(未选择课表)");
    } else {
        $("#course_study_layout").layout("panel", "south")
            .panel("setTitle", "课程学习情况(未选择课表)");
    }

    //初始化“南布局”内容
    initialSouthLayoutContent();

    //打印课表功能
    var print = $("#print");
    if (print) {
        print.click(function () {
            $(createPrintPage("个人教师课表", $("#course_table"))).jqprint();
        });
    }

    $("#blackListManage").click(function () {
        initialBlackListManagementDialog();
    });
});

function createPrintPage(strPrintName, printDatagrid) {
    var printAreaElement = "<div id='printArea'>";

    //加载标题
    printAreaElement+="<div style='text-align: center;font-size: 25px;'><b>个人教师课表</b></div>";

    //加载时间、姓名信息
    printAreaElement += "<div style='text-align: right;'><label>姓名:</label><span><b>"+ $("#student_name").val()+"</b></span>"
    var nowDate = new Date();
    var nowYear = nowDate.getFullYear();
    var nowMonth = nowDate.getMonth();
    var nowDay=nowDate.getDate();
    printAreaElement += "<label style='margin-left: 15px'>打印时间:</label><span><b>"+ nowYear+'-'+(nowMonth+1)+'-'+nowDay+"</b></span></div>";

    printAreaElement += "<table border='1' align='center' cellpadding='1' cellspacing='0' bordercolor='#000000'>";

    //加载列
    printAreaElement += "<tr><th rowspan='2' scope='row' style='width: 100px'>课程节次</th><th colspan='7' scope='col'>课程周期</th></tr>";
    printAreaElement += "<tr><th style='width: 150px'>周一</th><th style='width: 150px'>周二</th><th style='width: 150px'>周三</th>" +
        "<th style='width: 150px'>周四</th><th style='width: 150px'>周五</th><th style='width: 150px'>周六</th><th style='width: 150px'>周日</th></tr>";

    // 载入内容
    var rows = printDatagrid.datagrid("getRows"); // 这段代码是获取当前页的所有行。
    for (var j = 0; j < rows.length; j++) {
        printAreaElement += "<tr>";
        printAreaElement += "<td style='text-align: center'>" + rows[j]['courseTime'] + "</td>";
        printAreaElement += fillPrintCourseTableData(rows[j]['mondayCourse']);
        printAreaElement += fillPrintCourseTableData(rows[j]['tuesdayCourse']);
        printAreaElement += fillPrintCourseTableData(rows[j]['wednesdayCourse']);
        printAreaElement += fillPrintCourseTableData(rows[j]['thursdayCourse']);
        printAreaElement += fillPrintCourseTableData(rows[j]['fridayCourse']);
        printAreaElement += fillPrintCourseTableData(rows[j]['saturdayCourse']);
        printAreaElement += fillPrintCourseTableData(rows[j]['sundayCourse']);
        printAreaElement += "</tr>";
    }
    printAreaElement += "</table>";

    printAreaElement += "</div>";
    return printAreaElement;
}
function fillPrintCourseTableData(weekCourseData) {
    var printCourseWeekCell = "<td style='text-align: center;width: 100px'>";
    if (weekCourseData) {
        if (weekCourseData.courseName != undefined && weekCourseData.courseName != null) {
            printCourseWeekCell += weekCourseData.courseName;
            if (weekCourseData.classroomLocation != undefined && weekCourseData.classroomLocation != null) {
                printCourseWeekCell += "<br><span style='color:#888;font-size: 10px'>授课地点：" + weekCourseData.classroomLocation + "</span>";
            }
        }
    }
    printCourseWeekCell += "</td>";
    return printCourseWeekCell;
}

//初始化选课课程表
function initialCourseTable() {
    $("#course_table").datagrid({
        fit: true,
        rownumbers: false,
        singleSelect: true,
        pagination: false,
        title: "教师课程表",
        url: 'study/list/table',
        toolbar: '#tb',
        columns: [
            [
                {field: "courseTime", width: 100, align: 'center', title: '课程节次', rowspan: 2},
                {align: 'center', title: '课程周期', colspan: 7}
            ],
            [
                {field: "mondayCourse", width: 150, align: 'center', title: '周一', formatter: weekCourseFormatter},
                {field: "tuesdayCourse", width: 150, align: 'center', title: '周二', formatter: weekCourseFormatter},
                {field: "wednesdayCourse", width: 150, align: 'center', title: '周三', formatter: weekCourseFormatter},
                {field: "thursdayCourse", width: 150, align: 'center', title: '周四', formatter: weekCourseFormatter},
                {field: "fridayCourse", width: 150, align: 'center', title: '周五', formatter: weekCourseFormatter},
                {field: "saturdayCourse", width: 150, align: 'center', title: '周六', formatter: weekCourseFormatter},
                {field: "sundayCourse", width: 150, align: 'center', title: '周日', formatter: weekCourseFormatter}
            ]
        ],
        onClickCell: courseTableClickCell,
        onClickRow:function(rowIndex, rowData){
            var panel = $('#course_table').datagrid('getPanel');
            var tr = panel.find('div.datagrid-body tr[datagrid-row-index="'+rowIndex+'"]');
            tr.removeClass("datagrid-row-selected");
        }
    });
};

function initialSouthLayoutContent() {
    if (isElective == "true") {
        initialCourseStudentListTable();
    } else {
        initialCourseStudyTable();
    }
};

function initialCourseStudentListTable() {
    $("#course_student_list_table").datagrid({
        fit: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize: 10,
        columns: [
            [
                {field: 'studentNumber', title: '学号', align: 'center', width: 50},
                {field: 'name', title: '姓名', align: 'center', width: 80},
                {field: 'sex', title: '性别', align: 'center', width: 40,
                    formatter: function (value, row, index) {
                        return sex[value];
                    }
                },
                {field: 'identityCardNumber', title: '身份证号码', align: 'center', width: 150},
                {field: 'birthday', title: '生日', align: 'center', width: 80},
                {field: 'nation', title: '民族', align: 'center', width: 60,
                    formatter: function (value, row, index) {
                        return nation[value-1];
                    }
                },
                {field: 'executiveClass', title: '所在行政班', align: 'center', width: 100,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < adminClaszs.length; i++) {
                                if (adminClaszs[i].id == value) {
                                    return adminClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                },
                {field: 'teachingClass', title: '所在教学班', align: 'center', width: 100,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < studyClaszs.length; i++) {
                                if (studyClaszs[i].id == value) {
                                    return studyClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                }
            ]
        ],
        loadFilter:pageLoadFilter
    });

}

function initialCourseStudyTable() {
    $("#course_study_log_table").datagrid({
        fit: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize: 10,
        view: detailview,
        detailFormatter: function (index, row) {
            return '<div id="ddv-' + index + '" style="padding:5px 0"></div>';
        },
        onExpandRow: function (index, row) {

            if ((courseRow_time == undefined) || (courseRow_week == undefined)) {
                $.messager.alert("操作提示", "请先单击课程表", "warning");
            } else {
                var courseRows = $("#course_table").datagrid("getData");
                var courseCell = courseRows.rows[courseRow_time][courseRow_week];
                if (courseCell.scheduleId != 0) {
                    $('#ddv-' + index).panel({
                        height: 80,
                        border: false,
                        cache: false,
                        href: 'study/count/course/' + courseCell.scheduleId + '/' + electiveWeek + '/' + electiveTime + '/student/' + row.id + '/log',
                        onLoad: function () {
                            $('#course_study_log_table').datagrid('fixDetailRowHeight', index);
                        }
                    });
                    $('#course_study_log_table').datagrid('fixDetailRowHeight', index);
                }
            }
        },
        toolbar: [
            {
                text: '打印课程点名表',
                iconCls: 'icon-print',
                handler: function () {
                    if (courseName) {

                        $(createTimeSheetPrintPage(courseName, $("#course_study_log_table"))).jqprint();
                    } else {
                        $.messager.alert("操作提示", "请先单击有内容的课程表", "warning");
                    }
                }
            },
            '-',
            {
                text: '出勤记录',
                iconCls: 'icon-add',
                handler: function () {
                    if (courseName) {
                        $("#log_dialog").dialog("setTitle", courseName + "出勤记录-" + weekTitle + "(" + timeTitle + ")");
                        $("#log_dialog").dialog("open");
                        initialStudyLog(1);
                    } else {
                        $.messager.alert("操作提示", "请先单击有内容的课程表", "warning");
                    }
                }
            },
            '-',
            {
                text: '课堂表现记录',
                iconCls: 'icon-add',
                handler: function () {
                    if (courseName) {
                        $("#log_dialog").dialog("setTitle", courseName + "课堂表现记录-" + weekTitle + "(" + timeTitle + ")");
                        $("#log_dialog").dialog("open");
                        initialStudyLog(2);
                    } else {
                        $.messager.alert("操作提示", "请先单击有内容的课程表", "warning");
                    }
                }
            },
            '-',
            {
                text: '作业记录',
                iconCls: 'icon-add',
                handler: function () {
                    if (courseName) {
                        $("#log_dialog").dialog("setTitle", courseName + "作业记录-" + weekTitle + "(" + timeTitle + ")");
                        $("#log_dialog").dialog("open");
                        initialStudyLog(3);
                    } else {
                        $.messager.alert("操作提示", "请先单击有内容的课程表", "warning");
                    }
                }
            },
            '-',
            {
                text: '测验记录',
                iconCls: 'icon-add',
                handler: function () {
                    if (courseName) {
                        $("#log_dialog").dialog("setTitle", courseName + "平时测试记录-" + weekTitle + "(" + timeTitle + ")");
                        $("#log_dialog").dialog("open");
                        initialStudyLog(4);
                    } else {
                        $.messager.alert("操作提示", "请先单击有内容的课程表", "warning");
                    }
                }
            }
        ],
        columns: [
            [
                {field: 'studentNumber', title: '学号', align: 'center', width: 50},
                {field: 'name', title: '姓名', align: 'center', width: 80},
                {field: 'sex', title: '性别', align: 'center', width: 40,
                    formatter: function (value, row, index) {
                        return sex[value];
                    }
                },
                {field: 'identityCardNumber', title: '身份证号码', align: 'center', width: 150},
                {field: 'birthday', title: '生日', align: 'center', width: 80},
                {field: 'nation', title: '民族', align: 'center', width: 60,
                    formatter: function (value, row, index) {
                        return nation[value-1];
                    }
                },
                {field: 'executiveClass', title: '所在行政班', align: 'center', width: 100,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < adminClaszs.length; i++) {
                                if (adminClaszs[i].id == value) {
                                    return adminClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                },
                {field: 'teachingClass', title: '所在教学班', align: 'center', width: 100,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < studyClaszs.length; i++) {
                                if (studyClaszs[i].id == value) {
                                    return studyClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                },
                {field: 'id', align: 'center', width: 100,
                    formatter: function (value, row) {
                        if (value > 0) {
                            $("#course_study_log_table").oneTime('0.2s', function () {
                                $("#remove_student_" + value).linkbutton({
                                    iconCls: "icon-remove",
                                    plain: true
                                }).click(function () {
                                        $.messager.confirm("Delete确认",
                                            "从学生选课中Delete学生将会导致学生选课的相关所有信息将会丢失，并且该名学生将会被添加到黑名单，请确认Delete？",
                                            function (r) {
                                                if (r) {
                                                    var rowData = $("#course_study_log_table").datagrid("getSelected");
                                                    var rowIndex = $("#course_study_log_table").datagrid("getRowIndex", rowData);
                                                    $.ajax({
                                                        type: "POST",
                                                        url: "study/course/" + scheduleId + "/" + electiveWeek + "/" + electiveTime + "/remove/student/" + rowData.id,
                                                        dataType: "json",
                                                        cache: false,
                                                        success: function (data) {
                                                            $.messager.alert("操作提示", "数据删除成功！", "info");
                                                            $("#course_study_log_table").datagrid("deleteRow", rowIndex);
                                                        }
                                                    });
                                                }
                                            });
                                    });
                            });
                            return "<a id='remove_student_" + value + "' href='#'>Delete</a>";
                        }
                    }
                }
            ]
        ],
        loadFilter:pageLoadFilter
    });
    $("#dt").datebox({
        required:true
    });
    $("#date_selector_cancel").click(function(){
        $("#date_selector_dlg").dialog("close");
    });
    $("#date_selector_submit").click(function(){
        var date= $("#dt").datebox("getValue");
        if (!$("#log_tabs").tabs('exists', date)) {
            $.ajax({
                type: "POST",
                url: "study/add/course/" + scheduleId + "/" + electiveWeek + "/" + electiveTime + "/log/" + log_type + "/" + date,
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data) {
                        fillStudyLog(date);
                        $("#log_tabs").tabs("select", date);
                        $("#date_selector_dlg").dialog("close");
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $.messager.alert("操作提示", "数据保存异常:[" + jqXHR.statusText + "]", "error");
                }
            });
        }else{
            $("#log_tabs").tabs("select", date);
            $("#date_selector_dlg").dialog("close");
        }
    });
    $("#log_dialog").dialog({
        title: "未定义",
        iconCls: 'icon-edit',
        width: 600,
        height: 400,
        modal: true,
        toolbar:[{
            text:"添加日志",
            iconCls:'icon-add',
            handler:function(){
                var date=new Date();
                var dateString=date.getFullYear()+'-'+Append_zore(date.getMonth()+1)+'-'+Append_zore(date.getDate());
                $("#dt").datebox("setValue",dateString);
                $("#date_selector_dlg").dialog("open");
            }
        },"-",{
            text:"删除日志",
            iconCls:'icon-remove',
            handler:function(){
                var tab = $('#log_tabs').tabs('getSelected');
                if (tab) {
                    var title = tab.panel("options").title;
                    $.messager.confirm('删除确认', '您确定要删除“'+title+'”这一天的日志？', function(r){
                        if (r){
                            $.ajax({
                                type: "POST",
                                url: "study/delete/course/" + scheduleId + "/" + electiveWeek + "/" + electiveTime + "/log/" + log_type + "/" + title,
                                dataType: "json",
                                cache: false,
                                success: function (data) {
                                    if (data) {
                                        var index = $('#log_tabs').tabs('getTabIndex', tab);
                                        $('#log_tabs').tabs('close', index);
                                    }

                                }
                            });
                        }
                    });
                }
            }
        }]
    });
    $("#log_dialog").dialog("close");
    $("#log_tabs").tabs({
        fit: true,
        plain: true,
        tabPosition: "left",
        onSelect: function (title, index) {
            var tableId = "log_table_" + log_type + "_" + scheduleId + "_" + electiveWeek + "_" + electiveTime + "_" + title;
            $("#" + tableId).edatagrid({
                tools: [
                    {
                        iconCls: 'icon-edit',
                        handler: function () {
                            $("#" + tableId).edatagrid("editRow", $("#" + tableId).edatagrid("getRowIndex", $("#" + tableId).edatagrid("getSelected")));
                        }
                    },

                    {
                        iconCls: 'icon-save',
                        handler: function () {
                            $("#" + tableId).edatagrid("saveRow");
                        }
                    },'-'
                ],
                url: "study/list/course/" + scheduleId + "/" + electiveWeek + "/" + electiveTime + "/student/log/" + log_type + "/" + title,
                updateUrl: "study/student/course/log/update"
            });
        }
    });
}

function initialStudyLog(logType) {
    var tabs = $("#log_tabs").tabs("tabs");
    for (var i = tabs.length - 1; i >= 0; i--) {
        $("#log_tabs").tabs("close", 0);
    }
//    $.each(tabs, function (index, tab) {
//        $("#log_tabs").tabs("close", tab.panel('options').title);
//    });
    log_type = logType;
    $.ajax({
        type: "GET",
        url: "study/list/course/" + scheduleId + "/" + electiveWeek + "/" + electiveTime + "/log/" + logType + "/date",
        dataType: "json",
        cache: false,
        success: function (data) {
            if (data) {
                $.each(data, function (i, val) {
                    if (!$("#log_tabs").tabs('exists', val)) {
                        fillStudyLog(val);
                    }
                });
                $("#log_tabs").tabs("select", 0);
            }

        }
    });
}

function fillStudyLog(logDate) {
    var tableId = "log_table_" + log_type + "_" + scheduleId + "_" + electiveWeek + "_" + electiveTime + "_" + logDate;
    var dataGridTitle = {
        1: "出勤记录",
        2: "课堂表现记录",
        3: "作业记录",
        4: "平时测试记录"
    }[log_type] || undefined;

    var tab_options = {
        title: logDate,
        closable: false,
        content: "<table id=" + tableId + " title=" + dataGridTitle + " singleSelect=\"true\">" +
            "<thead>" +
            "<tr>" +
            "<th field=\"logId\" width=\"100\" hidden='true'></th>" +
            "<th field=\"studentName\" width=\"100\">学生姓名</th>" +
            "<th field=\"logValue\" width=\"100\" align=\"center\" editor=\"{type:'checkbox',options:{on:1,off:0}}\" " +
            "formatter=booleanFormatter>是否合格</th>" +
            "<th field=\"logContent\" width=\"200\" editor=\"text\">记录内容</th>" +
            "</tr>" +
            "</thead></table>"
    };
    $("#log_tabs").tabs("add", tab_options);
}

function createTimeSheetPrintPage(strPrintName, printDatagrid) {
    var printAreaElement = "<div id='printArea'>";

    //加载标题
    printAreaElement+="<div style='text-align: center;font-size: 25px;'><b>选修课点名表</b></div>";

    //加载时间、姓名信息
    printAreaElement += "<div style='text-align: right;'><label>姓名:</label><span><b>"+ $("#student_name").val()+"</b></span>"
    printAreaElement += "<label style='margin-left: 15px'>课程名称:</label><span><b>"+ strPrintName+"</b></span>";
    var nowDate = new Date();
    var nowYear = nowDate.getFullYear();
    var nowMonth = nowDate.getMonth();
    var nowDay=nowDate.getDate();
    printAreaElement += "<label style='margin-left: 15px'>打印时间:</label><span><b>"+ nowYear+'-'+(nowMonth+1)+'-'+nowDay+"</b></span></div>";

    printAreaElement += "<table border='1' align='center' cellpadding='1' cellspacing='0' bordercolor='#000000'>";

    //加载列
    printAreaElement += "<tr><th style='width: 40px'>序号</th><th style='width: 50px'>姓名</th><th style='width: 50px'>签到</th><th style='width: 150px'>备注</th>" +
        "<th style='width: 40px'>序号</th><th style='width: 50px'>姓名</th><th style='width: 50px'>签到</th><th style='width: 150px'>备注</th>" +
        "</tr>";

    // 载入内容
    var rows = printDatagrid.datagrid("getData").originalRows; // 这段代码是获取当前页的所有行。
    for (var j = 0; j < rows.length; j++) {
        if(j%2==0){
            printAreaElement += "<tr>";
        }

        printAreaElement += "<td style='text-align: center;width;100px'>" + (j+1) + "</td>";
        printAreaElement +=fillPrintCourseTimeSheetData(rows[j]);
        if(j%2!=0){
            printAreaElement += "</tr>";
        }
    }
    printAreaElement += "</table>";

    printAreaElement += "</div>";
    return printAreaElement;
}
function fillPrintCourseTimeSheetData(TimeSheetData) {
    var printTimeSheetCell = "";
    if (TimeSheetData) {
        printTimeSheetCell += "<td style='text-align: center;widows: 100px;'>";
        printTimeSheetCell+=TimeSheetData.name;
        printTimeSheetCell += "</td><td style='text-align: center'></td><td style='text-align: center'></td>";
    }
    return printTimeSheetCell;
}

var courseRow_week=undefined;
var courseRow_time=undefined;

//点击选课课程表单元格事件处理
function courseTableClickCell(rowIndex, field, value) {
    if (field == "courseTime" || $("#course_student_list_table") == null) {
        return;
    }

    var panel = $('#course_table').datagrid('getPanel');
    var selectTd = panel.find('div.datagrid-body tr td.datagrid-row-selected');
    selectTd.removeClass("datagrid-row-selected");
    var tr = panel.find('div.datagrid-body tr[datagrid-row-index="'+rowIndex+'"]');
    tr.each(function(){
        //添加选择的单元格样式
        var td = tr.children('td[field="'+field+'"]');
        td.addClass("datagrid-row-selected");
    });

    electiveWeek = {
        mondayCourse: 1,
        tuesdayCourse: 2,
        wednesdayCourse: 4,
        thursdayCourse: 8,
        fridayCourse: 16,
        saturdayCourse: 32,
        sundayCourse: 64
    }[field] || 0;
    weekTitle = {
        mondayCourse: "周一",
        tuesdayCourse: "周二",
        wednesdayCourse: "周三",
        thursdayCourse: "周四",
        fridayCourse: "周五",
        saturdayCourse: "周六",
        sundayCourse: "周日"
    }[field] || "";
    timeTitle = $("#course_table").datagrid("getRows")[rowIndex]["courseTime"];
    electiveTime = {
        "第一节课": 1,
        "第二节课": 2,
        "第三节课": 4,
        "第四节课": 8,
        "第五节课": 16,
        "第六节课": 32,
        "第七节课": 64,
        "第八节课": 128
    }[timeTitle] || 0;
    courseRow_time=rowIndex;
    courseRow_week = {
        1: "mondayCourse",
        2: "tuesdayCourse",
        4: "wednesdayCourse",
        8: "thursdayCourse",
        16: "fridayCourse",
        32: "saturdayCourse",
        64: "sundayCourse"
    }[electiveWeek] || undefined;

    scheduleId = value.scheduleId;

    if (isElective == "true") {
        loadCourseStudentListData(value.scheduleId, electiveWeek, electiveTime);
    } else {
        loadCourseLogStudentListData(value.scheduleId, electiveWeek, electiveTime);
    }
    courseName = value.courseName;
    if (courseName) {
        $("#course_study_layout").layout("panel", "south")
            .panel("setTitle", courseName + "学生列表-" + weekTitle + "(" + timeTitle + ")");
    } else {
        $("#course_study_layout").layout("panel", "south")
            .panel("setTitle", "学生列表-" + weekTitle + "(" + timeTitle + ")");
    }
};

//教师课表格式
function weekCourseFormatter(value, row, index) {
    var returnValue = "";
    if (value) {
        var courseName = value.courseName;
        var classroomLocation = value.classroomLocation;
        if (courseName) {
            returnValue += "<span style='font-weight:bold'>" + courseName + "</span>";
            if (classroomLocation) {
                returnValue += "<br><span style='color:#888'>授课地点：" + classroomLocation + "</span>";
            }
        }
    }
    return  returnValue;
};

function loadCourseStudentListData(scheduleId, week, time) {
    $("#course_student_list_table").datagrid("loadData", []);
    $("#course_student_list_table").datagrid("options").pageNumber = 1;
    $("#course_student_list_table").datagrid('getPager').pagination({pageNumber: 1});
    $("#course_student_list_table").datagrid({
        url: "study/list/course/" + scheduleId + "/" + week + "/" + time
    });
//    $.ajax({
//        type: "GET",
//        url: "study/list/course/" + scheduleId + "/" + week + "/" + time,
//        dataType: "json",
//        cache: false,
//        success: function (data) {
//            $("#course_student_list_table").datagrid("loadData", data);
//        }
//    });
};

function loadCourseLogStudentListData(scheduleId, week, time) {
    $("#course_study_log_table").datagrid("loadData", []);
    $("#course_study_log_table").datagrid("options").pageNumber = 1;
    $("#course_study_log_table").datagrid('getPager').pagination({pageNumber: 1});
    $("#course_study_log_table").datagrid({
        url: "study/list/course/" + scheduleId + "/" + week + "/" + time
    });
//    $.ajax({
//        type: "GET",
//        url: "study/list/course/" + scheduleId + "/" + week + "/" + time,
//        dataType: "json",
//        cache: false,
//        success: function (data) {
//            $("#course_study_log_table").datagrid("loadData", []);
//            $("#course_study_log_table").datagrid("options").pageNumber = 1;
//            $("#course_study_log_table").datagrid('getPager').pagination({pageNumber: 1});
//            $("#course_study_log_table").datagrid("loadData", data);
//        }
//    });
};

function initialBlackListManagementDialog() {
    $("#manageBlackListDialog").dialog({
        title: "选课黑名单管理",
        iconCls: 'icon-edit',
        width: 500,
        height: 400,
        modal: true
    });
    $("#studentsInBlackListDataGrid").datagrid({
        title: "黑名单学生列表",
        fitColumns: true,
        fit: true,
        idField: 'id',
        sortName: 'id',
        sortOrder: 'desc',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize: 10,
        border: true,
        url: "blackList/list/students",
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    $("#add_student_dlg").dialog("open");
                    $('#student_combo').combogrid({
                        panelWidth: 450,
                        idField: 'id',
                        textField: 'name',
                        url: 'blackList/list/notIn/student',
                        columns: [
                            [
                                {field: 'studentNumber', title: '学号', align: 'center', width: 50},
                                {field: 'name', title: '姓名', align: 'center', width: 100},
                                {field: 'sex', title: '性别', align: 'center', width: 40,
                                    formatter: function (value, row, index) {
                                        return sex[value];
                                    }
                                },
                                {field: 'nation', title: '民族', align: 'center', width: 50,
                                    formatter: function (value, row, index) {
                                        return nation[value-1];
                                    }
                                },
                                {field: 'executiveClass', title: '所在行政班', align: 'center', width: 80,
                                    formatter: function (value, row) {
                                        if (value > 0) {
                                            for (var i = 0; i < adminClaszs.length; i++) {
                                                if (adminClaszs[i].id == value) {
                                                    return adminClaszs[i].name;
                                                }
                                            }
                                        } else
                                            return '';
                                    }
                                },
                                {field: 'teachingClass', title: '所在教学班', align: 'center', width: 80,
                                    formatter: function (value, row) {
                                        if (value > 0) {
                                            for (var i = 0; i < studyClaszs.length; i++) {
                                                if (studyClaszs[i].id == value) {
                                                    return studyClaszs[i].name;
                                                }
                                            }
                                        } else
                                            return '';
                                    }
                                }
                            ]
                        ],
                        fitColumns: true
                    });
                    $("#add_student_submit").click(function () {
                        var studentId = $('#student_combo').combogrid('getValue');
                        if (studentId) {
                            $.ajax({
                                type: 'POST',
                                url: "blackList/add/" + studentId,
                                dataType: "json",
                                cache: false,
                                success: function (data) {
                                    $("#studentsInBlackListDataGrid").datagrid("reload");
                                }
                            });
                        }
                        $('#student_combo').combogrid("clear");
                        $("#add_student_dlg").dialog("close");
                    });
                }

            }
        ],
        columns: [
            [
                {field: 'studentNumber', title: '学号', align: 'center', width:50},
                {field: 'name', title: '姓名', align: 'center', width: 100},
                {field: 'sex', title: '性别', align: 'center', width: 40,
                    formatter: function (value, row, index) {
                        return sex[value];
                    }
                },
                {field: 'nation', title: '民族', align: 'center', width: 60,
                    formatter: function (value, row, index) {
                        return nation[value-1];
                    }
                },
                {field: 'executiveClass', title: '所在行政班', align: 'center', width: 80,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < adminClaszs.length; i++) {
                                if (adminClaszs[i].id == value) {
                                    return adminClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                },
                {field: 'teachingClass', title: '所在教学班', align: 'center', width: 80,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < studyClaszs.length; i++) {
                                if (studyClaszs[i].id == value) {
                                    return studyClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                },
                {field: "id", width: 200, align: 'center',
                    formatter: function (value, row, index) {
                        if (row) {
                            $("#studentsInBlackListDataGrid").oneTime('0.2s', function () {
                                $("#remove_student_" + row.id).linkbutton({
                                    iconCls: "icon-remove",
                                    plain: true
                                });
                                $("#remove_student_" + row.id).click(function () {
                                    $.ajax({
                                        type: 'POST',
                                        url: "blackList/delete/" + row.id,
                                        dataType: "json",
                                        cache: false,
                                        success: function (data) {
                                            $("#studentsInBlackListDataGrid").datagrid("reload");
                                        }
                                    });
                                });
                            });
                            return "<a id='remove_student_" + row.id + "' href='#'>删除学生</a>";
                        }
                    }
                }
            ]
        ],
        loadFilter:pageLoadFilter
    });
}

//布尔值格式化显示
function booleanFormatter(value, row, index) {
    if (String(value) == "1") {
        return "是";
    }
    return "否";
};

$.extend($.fn.layout.methods, {
    setRegionToolVisibleState:function(jq,params){
        return jq.each(function(){
            if(params.region=="center")
                return;
            var panels = $.data(this, 'layout').panels;
            var panel = panels[params.region];
            var tool = panel.panel('header').find('>div.panel-tool');
            tool.css({display:params.visible?'block':'none'});
            var first = params.region.substring(0,1);
            var others = params.region.substring(1);
            var expand = 'expand' + first.toUpperCase() + others;
            if(panels[expand]){
                panels[expand].panel('header').find('>div.panel-tool').css({display:params.visible?'block':'none'});
            }
        });
    }
});

function Append_zore(temp)
{
    if(temp<10)
    {
        return "0"+temp;
    }
    else
    {
        return temp;
    }
}
